package com.controller;

import com.dto.BaseDto;
import com.entity.Goods;
import com.page.PageInfo;
import com.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Controller
public class GoodsController {
    @Resource
    GoodsService goodsService;

//传统传参，不需要绑定参数 url直接在后面写上？adid=0001
    @RequestMapping("/goods/getByAdidJson")
    @ResponseBody
    public Goods getByAdidJson(String gid) {
        return goodsService.getByGid(gid);
    }

//rest风格传参 将参数变为url的一部分但是需要绑定 例如 /goods/getByAdidNew/0001
    @RequestMapping("/goods/getByAdidNewJSon/{gid}")
    @ResponseBody
    public Goods getByAdidNewJson(@PathVariable("gid") String gid) {
        return goodsService.getByGid(gid);
    }

    @RequestMapping("/goods/getAllJson")
    @ResponseBody
    public BaseDto<Goods> getAllJson() {
        return goodsService.getAll();

    }

    @RequestMapping("/goods/getAll")
    public ModelAndView getAll() {
       BaseDto<Goods> dto = goodsService.getAll();
        return new ModelAndView("/back/goods/list","dto",dto);

    }

    @RequestMapping("/goods/getAllByPage")
    public ModelAndView getAllByPage(PageInfo pageInfo) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Goods> dto = goodsService.getAllByPage(pageInfo);
        ModelAndView mv = new ModelAndView("/back/goods/list","dto",dto);
        return mv;
    }
    @RequestMapping("/goods/getAllByPageJson")
    @ResponseBody
    public BaseDto getAllByPageJson(PageInfo pageInfo) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Goods> dto = goodsService.getAllByPage(pageInfo);
        return dto;
    }


    @RequestMapping("/goods/searchNameByPage/{adname}")
    @ResponseBody
    public BaseDto searchNameByPage(PageInfo pageInfo,@PathVariable("adname")String adname) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Goods> dto = goodsService.searchNameByPage(pageInfo,adname);
        return dto;
    }


    /**
     * 根据主键查询
     * @param gid
     * @return
     */
    @RequestMapping("/goods/getGoodsByGid/{gid}")
    @ResponseBody
    public BaseDto getGoodsByAdid(@PathVariable("gid") String gid){
        return goodsService.getGoodsByGid(gid);
    }

    /**
     * 添加一个实体
     * @param goods
     * @return
     */
    @RequestMapping("/goods/addGoods")
    @ResponseBody
    public BaseDto addGoods(Goods goods){
        return goodsService.addGoods(goods);
    }
    /**
     * 修改实体
     */
    @RequestMapping("/goods/updateGoods")
    @ResponseBody
    public BaseDto updateGoods(Goods goods){
        return goodsService.updateGoods(goods);
    }
    public BaseDto updateGoodsByExample(){
        return null;
    }

    /**
     * 删除一个实体
     * @param gid
     * @return
     */
    @RequestMapping("/goods/deleteGoodsByPrimayKey/{gid}")
    @ResponseBody
    public BaseDto deleteGoodsByPrimayKey(@PathVariable("gid") String gid){
        return goodsService.deleteGoodsByPrimayKey(gid);
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public BaseDto deleteGoodsByExample(){
        return null;
    }
}
